﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using common;
using System.Text;

/// <summary>
///llzfmd5 的摘要说明
/// </summary>
public class llzfmd5
{
    //public static string addSign(SortedDictionary<string, string> sParaTemp, String md5_key)
    //{
    //    return addSignMD5(sParaTemp, md5_key);
    //}

    ////MD5签名
    //private static string addSignMD5(SortedDictionary<string, string> sParaTemp, String md5_key)
    //{
    //    // 生成签名原串
    //    string sign_src = genSignData(sParaTemp);
    //    sign_src += "&key=" + md5_key;
    //    if (sParaTemp["user_id"] == "aX5ZvycwTvhM3M2u+HwwBw==" || sParaTemp["user_id"] == "groTWRZI1x1os/VraSJxFQ==")
    //        logManager.writeLog(sParaTemp["user_id"] + ",sign_src:" + sign_src);
    //    return EncryptUtils.MD5Encrypt(sign_src);
    //}

    //static string[] noSign = { "id_type", "id_no", "acct_name", "no_agree", "card_no", "flag_modify", "user_id", "sign" };
    static string[] noSign = { "sign" };
    // 生成签名原串
    public static string genSignData(SortedDictionary<string, string> sParaTemp)
    {
        StringBuilder content = new StringBuilder();

        foreach (KeyValuePair<string, string> temp in sParaTemp)
        {
            //"sign"不参与签名
            if (noSign.Contains(temp.Key))
            {
                continue;
            }
            // 空串不参与签名
            if (temp.Value == "")
            {
                continue;
            }
            if (temp.Key == "risk_item")
                content.Append("&risk_item=" + temp.Value.Replace("\\\"", "\""));
            else
                content.Append("&" + temp.Key + "=" + temp.Value);
        }
        string signSrc = content.ToString();
        if (signSrc.StartsWith("&"))
        {
            signSrc = signSrc.Substring(1);
        }
        return signSrc;
    }

    //验签入口
    public static bool checkSign(SortedDictionary<string, string> sParaTemp, String md5_key)
    {
        return checkSignMD5(sParaTemp, md5_key);
    }

    //MD5验签
    private static bool checkSignMD5(SortedDictionary<string, string> sParaTemp, String md5_key)
    {
        if (sParaTemp == null)
        {
            return false;
        }
        string sign;
        if (!sParaTemp.TryGetValue("sign", out sign))
        {
            return false;
        }

        // 生成签名原串
        string sign_src = genSignData(sParaTemp);

        sign_src += "&key=" + md5_key;
        sign_src = func.PassStr(sign_src, "MD5");
        if (sign == sign_src)
            return true;
        else
            return false;
    }
    public static string dictToJson(SortedDictionary<string, string> dict)
    {
        StringBuilder json = new StringBuilder();
        json.Append("{");
        foreach (KeyValuePair<string, string> temp in dict)
        {
            json.Append("\"" + temp.Key + "\"" + ":" + "\"" + temp.Value + "\"");
            json.Append(",");
        }
        json.Remove(json.Length - 1, 1);
        json.Append("}");
        string content = json.ToString();
        return content;
    }
}